import Vue from "vue"


const wxlogin = {

	getuserinfo: function() {
		let that = this
		uni.getUserProfile({
			lang: 'zh_CN',
			desc: "用户注册",
			success: (res) => {
				console.log(res)
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						that.getuserid(loginRes) 
						uni.setStorageSync('wxuserinfo', res)
					}
				});
			},
			fail: res => {
				console.warn(res)
				let _this = this
				uni.showModal({
					title: '系统提示',
					content: '系统想要获取用户信息，请点击按钮马上登陆！',
					confirmText: '马上登陆',
					success: function(res) {
						if (res.confirm) {
							_this.getuserinfo()
						} else if (res.cancel) {
							uni.showToast({
								title:'用户拒绝授权'
							})
						}
					},
				})
			}
		})


	},
	getuserid(loginRes) {
		//修改成你的小程序信息
		let appid='wxee410f9237ce87da'
		let secret='f52db36a113b7011ef289ae089f7ea90'
		let url =
			'https://api.weixin.qq.com/sns/jscode2session?appid='+appid+'&secret='+secret+'&js_code=' +
			loginRes.code +
			'&grant_type=authorization_code'
		uni.showLoading({
			title: "登陆中..."
		})
		console.warn(url)
		uni.request({
			url: url,
			dataType: 'json',
			method: 'GET',
			success: (result) => {
				console.log(result.data.openid)
				uni.setStorageSync('wxuserid', result)
			},
			complete() {
				uni.hideLoading()
			}
		});
	}

}

export default wxlogin
